package my_web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Proxy.DynamicProxy;
import ReqBean.RequestBean;
import my_web.Interface.DepInterface;
import my_web.bean.Dep;
import my_web.model.DepDto;
import my_web.service.DepService;
public class DepServlet extends HttpServlet{
		//处理get方式的请求
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");//设置请求的编码格式
		//res.setCharacterEncoding("utf-8");//设置响应的编码格式
		res.setContentType("text/html;charset=UTF-8 ");
		//PrintWriter out=res.getWriter();
		String path=req.getServletPath();//获取请求路径--上下文为根的请求路径
		//1.创建业务对象
		//DepService  service=new DepService();
		DepInterface  service = null;
		try {
			service=DynamicProxy.creatServiceProxy(DepInterface.class, DepService.class);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if("/DepToList.do".equals(path)){
			try {
				DepDto dto=RequestBean.getBeanFromRequest(DepDto.class, req);
				req.setAttribute("pg",service.DynamicQueryAllDeps(dto));
				req.getRequestDispatcher("DepList.jsp").forward(req, res);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}else if("/DepToAdd.do".equals(path)){
			req.getRequestDispatcher("DepAdd.jsp").forward(req, res);
		}else if("/DepDoAdd.do".equals(path)){
			try {
				DepDto dto=RequestBean.getBeanFromRequest(DepDto.class, req);
				service.addDep(dto);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//重定向
			res.sendRedirect("DepToList.do");
		}else if("/DepDoDel.do".equals(path)){
			res.setContentType("application/json;charset=UTF-8");
			PrintWriter out=res.getWriter();
			try {
				String d=req.getParameter("d_id");
				service.removeDepById(d);
				out.print("{\"success\":true,\"msg\":\"删除成功！\"}");//操作成功
			} catch (Exception e) {
				e.printStackTrace();
				out.print("{\"success\":false,\"msg\":\"删除失败！\"}");//操作失败
			}
			out.flush();
			out.close();
			//res.sendRedirect("DepToList.do");	
		}else if("/DepDoMultiDel.do".equals(path)){
			String[]d=req.getParameterValues("d_ids");
			service.removeDepById(d);	
			res.sendRedirect("DepToList.do");
		}else if("/DepToUpd.do".equals(path)){
			//通过id从请求中得到需要修改的数据
			String d=req.getParameter("d_id");
			Dep dep=service.getDepById(d);
			req.setAttribute("dep", dep);
			req.getRequestDispatcher("DepUpd.jsp").forward(req, res);
		}else if("/DepDoUpd.do".equals(path)){
			DepDto dto;
			try {
				dto = RequestBean.getBeanFromRequest(DepDto.class, req);
				service.modifyDep(dto);
				//重定向
				res.sendRedirect("DepToList.do");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	//处理post方式的情况
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		this.doGet(req, res);
	}
}